package com.happy.second.Controller;

import com.happy.second.Entity.qlpEntity.User;
import com.happy.second.Service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author itoutsource.cz10
 */
@RestController
@RequestMapping(value = "/qlp")
public class WelcomeQlpController {

    private static final Logger log = LoggerFactory.getLogger(WelcomeQlpController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/welcome/{name}", method = RequestMethod.GET)
    public String welcome(@PathVariable(value = "name") String name) {
        log.info("welcome");
        return "welcome " + name;
    }


    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public String update(@RequestBody User user) {
        log.error("这是一个简单的错误");
        return "welcome " + user.getName();
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable(value = "id") int id) {
        User user = userService.selectByPrimaryQlp(id);
        log.info("userInfo" + user.toString());
        return user;
    }

    /**
     * list动态插入  错误一个 其他的也不插入
     **/
    @RequestMapping(value = "/user/insert", method = RequestMethod.POST)
    public int insert() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setName("name" + i);
            user.setEnable(false);
            user.setTel("123");
            if (i == 4) {
                user.setTel("1230000000000000000000000000000000000000000");
            }
            userList.add(user);
        }
        int count = userService.insertIntoUsers(userList);
        return count;
    }


    /**
     * 出错不会回滚  需要添加事务
     **/

    @RequestMapping(value = "/user/insertOne", method = RequestMethod.POST)
    public void insertOne() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setName("name" + i);
            user.setEnable(false);
            user.setTel("123");
            if (i == 4) {
                user.setTel("1230000000000000000000000000000000000000000");
            }
            userList.add(user);
        }
        userService.insertUser(userList);
    }

}
